package com.example.fashionshoes.interceptor;

import com.example.fashionshoes.constant.JwtConst;
import com.example.fashionshoes.utils.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.time.Duration;
import java.util.Map;

@Component
public class JwtFreshInterceptor implements HandlerInterceptor {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String token = request.getHeader(JwtConst.AUTH);
        if (token == null) {
            return true;
        }
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer userId = (Integer) map.get(JwtConst.USER_ID);
        stringRedisTemplate.expire(JwtConst.USER_TOKEN_CATCH + userId, Duration.ofMinutes(20));
        return true;
    }
}
